//
//  OCRViewControlller.swift
//  Entrance
//
//  Created by chunhuiLai on 2018/1/16.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//
import UIKit
import SYKit
import RxSwift
import RxCocoa
//import YourFilm

let width = UIScreen.sy_width - 32

class CardExhibitionView: UIView {
    
    lazy var textLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textAlignment = .center
        
        return label
    }()
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    lazy var certainButton: UIButton = {
        let button = UIButton.init(type: .custom)
        button.setTitle("确定", for: .normal)
        button.backgroundColor = UIColor.lightGray
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        layoutUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func layoutUI() {
    
        addSubview(imageView)
        addSubview(textLabel)
        addSubview(certainButton)
        
        textLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(10)
            make.right.equalTo(-10)
        }
        
        imageView.snp.makeConstraints { (make) in
            make.top.equalTo(textLabel.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalTo(-10)
            make.bottom.equalTo(-50)
        }
        
        certainButton.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(40)
        }
    }
}

class OCRViewControlller: UIViewController {
    
    var disposeBag = DisposeBag()
    
    lazy var imagePicher: UIImagePickerController = {
        let picker = UIImagePickerController()
        picker.modalTransitionStyle = .flipHorizontal
        picker.allowsEditing = true
        picker.delegate = self
        return picker
    }()
    
    lazy var container: CardExhibitionView = {
        let view = CardExhibitionView()
        view.sy_size = CGSize.init(width: width, height: width * 1.5)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if SYDeviveAuthorization.captureDeviceDidAuthorized() {
            //imagePicher
            imagePicher.sourceType = UIImagePickerControllerSourceType.camera
            
            imagePicher.cameraCaptureMode = .photo
            
            present(imagePicher, animated: true, completion: nil)
        }
        else {
            printlog("媒体权限未打开")
        }
        
        view.backgroundColor = UIColor.white
    }
    
    func  display(image: UIImage) {
        
        dismiss(animated: true, completion: nil)
        
        container.certainButton.rx.tap.bind { _ in
            YourFilm.curtainCall()
        }
        .disposed(by: disposeBag)
    
        container.imageView.image = image
        var plot = YourFilm.Plot.default
        plot.showTimeDuration = TimeInterval(Int.max)
        YourFilm.show(container, plot: plot, scenery: YourFilm.Scenery.default, inView: nil)
    }
}

extension OCRViewControlller: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
//            if let resultImage = OpenCVWrapper.shared().opencvScanCard(image) {
//                display(image: resultImage)
//            }
            printlog(Thread.current)
                self.container.textLabel.text = OpenCVWrapper.shared().tesseractRecognize(with: image)
        
        }
    }
}
